package com.hzw.saas.common.util.enums;

import java.util.Objects;

/**
 * @author zzl
 * @since 04/19/2021
 */
public enum LogLevelEnum {

    TRACE(1, "TRACE"),
    DEBUG(2, "DEBUG"),
    INFO(3, "INFO"),
    WARN(4, "WARN"),
    ERROR(5, "ERROR"),
    FATAL(6, "FATAL"),
    OFF(7, "OFF");
    private final int code;
    private final String text;

    LogLevelEnum(int code, String text) {
        this.code = code;
        this.text = text;
    }

    public int code() {
        return code;
    }

    public String text() {
        return text;
    }

    public static LogLevelEnum instance(String text) {
        if (null == text)
            return null;
        text = text.trim().toUpperCase();
        for (LogLevelEnum value : LogLevelEnum.values()) {
            if (Objects.equals(value.text, text))
                return value;
        }
        return null;
    }

    public static boolean enableLog(LogLevelEnum baseLogLevel, LogLevelEnum tarLogLevel) {
        if (baseLogLevel == null)
            return true;
        if (tarLogLevel == null)
            return false;
        if (Objects.equals(baseLogLevel, LogLevelEnum.OFF))
            return false;
        return tarLogLevel.code - baseLogLevel.code >= 0;
    }
}
